package com.masterclient.app.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.eoner.baselibrary.utils.DensityUtil;
import com.masterclient.app.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 普通提示dialog
 */
public class CommonDialog extends Dialog {

    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.cancel_tv)
    TextView cancelTv;
    @BindView(R.id.confirm_tv)
    TextView confirmTv;

    /**
     * 确定按钮点击事件
     */
    public interface OnConfirmClickListener {
        void onConfirmClick();
    }

    /**
     * 取消按钮点击事件
     */
    public interface OnCancelClickListener {
        void onCancelClick();
    }

    private OnConfirmClickListener onConfirmClickListener;
    private OnCancelClickListener onCancelClickListener;
    private String title;// dialog标题
    private String comfirmTxt;// 确定按钮文字
    private String cancelTxt;// 取消按钮文字

    public CommonDialog(@NonNull Context context) {
        super(context, R.style.MyDialog);
    }

    public CommonDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_common);
        ButterKnife.bind(this);
        setCanceledOnTouchOutside(false);
        if (!TextUtils.isEmpty(title))
            titleTv.setText(title);
        if (!TextUtils.isEmpty(comfirmTxt))
            confirmTv.setText(comfirmTxt);
        if (!TextUtils.isEmpty(cancelTxt))
            cancelTv.setText(cancelTxt);
    }

    public void show() {
        super.show();
        Window window = getWindow();      // 得到dialog的窗体
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = (int) (DensityUtil.getScreenWidth(getContext()) * 0.85);
        getWindow().setAttributes(layoutParams);
    }


    @OnClick({R.id.cancel_tv, R.id.confirm_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cancel_tv:
                dismiss();
                if (onCancelClickListener != null)
                    onCancelClickListener.onCancelClick();
                break;
            case R.id.confirm_tv:
                dismiss();
                if (onConfirmClickListener != null)
                    onConfirmClickListener.onConfirmClick();
                break;
        }
    }

    public static class Builder {

        private CommonDialog mDialog;

        public Builder(Context context, int themeResId) {
            mDialog = new CommonDialog(context, themeResId);
        }

        public Builder(Context context) {
            mDialog = new CommonDialog(context);
        }

        /**
         * 设置dialog标题
         *
         * @param title
         * @return
         */
        public Builder setTitle(String title) {
            mDialog.title = title;
            return this;
        }

        /**
         * 设置文字确定和事件
         *
         * @param confirmTxt
         * @param onConfirmClickListener
         * @return
         */
        public Builder setOnConfirmClickListener(String confirmTxt, OnConfirmClickListener onConfirmClickListener) {
            mDialog.comfirmTxt = confirmTxt;
            mDialog.onConfirmClickListener = onConfirmClickListener;
            return this;
        }

        /**
         * 设置取消文字和事件
         *
         * @param cancelTxt
         * @param onCancelClickListener
         * @return
         */
        public Builder setOnCancelClickListener(String cancelTxt, OnCancelClickListener onCancelClickListener) {
            mDialog.cancelTxt = cancelTxt;
            mDialog.onCancelClickListener = onCancelClickListener;
            return this;
        }

        public CommonDialog createDialog() {
            return mDialog;
        }

    }

}
